﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MoeRabbit
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitData();
        }

        private void InitData()
        {
            Console.WriteLine(new FileExistence().DicExt(StaticValue.DataPath));
            Console.WriteLine(new FileExistence().DicExt(StaticValue.TempPath));
            File.AppendAllText(StaticValue.Oldurl1, "");
            Console.WriteLine(StaticValue.DataFile);
        }

        cat_select_nodes Html_select = new cat_select_nodes();
        Webclient_cat html_analysis = new Webclient_cat();
   

      
        private void button1_Click(object sender, EventArgs e)
        {
            List<String> down_load_list  = File.ReadAllLines(StaticValue.Oldurl1).ToList<string>();
            for (int page = 1; page < Convert.ToInt32(textBox1.Text); page++) {
                String Shtml = html_analysis.GetWebClient("http://pic.moetutu.com/page/" + page);
                Console.WriteLine("第{0}页正在扫描",page);
                foreach (HtmlNode moetutu in Html_select.Regex(Shtml, "//a[@class='thumb-holder-link']"))
                {
                    Console.WriteLine(moetutu.Attributes["href"].Value);

                    HtmlNode img_src = Html_select.Regex(html_analysis.GetWebClient(moetutu.Attributes["href"].Value), "//a[@class='Original_img_url']")[0];
                    string img = img_src.Attributes["href"].Value;
                    Console.WriteLine(img);
                    if (!down_load_list.Contains(img))
                    {
                        down_load_list.Add(img);
                        System.IO.File.AppendAllText(StaticValue.DataFile, "\r\n" + img);
                        System.IO.File.AppendAllText(StaticValue.Oldurl1, "\r\n" + img);
                    }
                    else {

                        Console.WriteLine("已存在，无需写入");
                    }
                }
            }
              new ListSplit().   List_Split(StaticValue.DataFile);

            StaticValue.DataFile =StaticValue.DataPath+ DateTime.Now.ToFileTime() + "List.lst";
            MessageBox.Show("全部任务完成！请查看data\\已经切割 文件夹下的list文件,双击即可唤起迅雷进行下载。");
        }

     

   

     
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键  
            {
                if ((e.KeyChar < '0') || (e.KeyChar > '9'))//这是允许输入0-9数字  
                {
                    e.Handled = true;
                }
            }
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Explorer.exe", StaticValue.DataPath);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;

            DialogResult dr = MessageBox.Show("确定要清理垃圾吗", "清理垃圾", messButton);

            if (dr == DialogResult.OK)//如果点击“确定”按钮
            {
                try
                {
                    new DirectoryInfo(StaticValue.DataPath).Delete(true);
                   
                    new FileExistence().DicExt(StaticValue.DataPath);
                    new FileExistence().DicExt(StaticValue.Listtemp1);
                    MessageBox.Show("已清空！");
                }
                catch (Exception ex){
                    MessageBox.Show(ex.Message);
                }
            }
           

        }
    }
}
